package com.bruce.demo2.patterns.demo1.creational.demo01.singleton.demo04;

/**
 * @PROJECT_NAME design_patterns
 * @DESCRIPTION 懒汉式：方式2
 * 优点：解决线程安全问题
 * 缺点：在方法获取上添加同步锁，会有性能损失
 * @AUTHOR CNBruceLee
 * @DATE 2021/7/1 22:59
 */
public class Singleton {
    //1、构造函数私有化
    private Singleton() {
    }

    //2、定义Singleton对象
    private static Singleton instance;

    //对外提供访问方式 为方法添加同步锁，用synchronized关键词修饰
    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

}
